package com.mxy.myapplication.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.support.design.widget.Snackbar;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.util.SparseArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
import com.bumptech.glide.load.resource.bitmap.BitmapEncoder;
import com.mxy.myapplication.R;

import java.util.ArrayList;

/**
 * 轮播ViewPager的Adapter
 * creat by 功夫小勇
 */

public class AdapterForBanner extends PagerAdapter {
    //这里一定要是能被2整除的数
    public final static int MAX_BANNER_SIZE = 2000000;
    private ArrayList<Integer> photolist;
    private Context cont;
    private LayoutInflater lf;
    // key为int 的时候推荐使用SparseArray，节省内存开销
    private SparseArray<View> viewmap = new SparseArray<>(3);
    private ViewPager bannervp;

    public AdapterForBanner(Context cont, ArrayList<Integer> photolist, ViewPager bannervp) {
        this.photolist = photolist;
        this.cont = cont;
        this.bannervp = bannervp;
        this.lf = LayoutInflater.from(cont);
    }

    @Override
    public int getCount() {
        return photolist.size() < 2 ? 1 : MAX_BANNER_SIZE;//只有一张图的话，不滑动
    }

    @Override
    public boolean isViewFromObject(View view, Object object) {
        return view == object;
    }

    @Override
    public Object instantiateItem(ViewGroup container, int position) {//Viewpager会自动初始化三页
        // (如果adapter元素多余三个的话)
        // ，即：上一页、当前页、下一页，并且保存三页内存，而且当用户或系统切换至当前页后，自动加载下一页跟上一页页面，前提是下一页或上一页没有被创建,否则只创建尚未创建的页面
        View v = this.lf.inflate(R.layout.item_bannerviewpagerphoto, null, false);
        ImageView iv = (ImageView) v.findViewById(R.id.iv);
        int photoposition = getPhotoPosition(position, photolist.size());
        v.setOnClickListener(new Itemclicklistener(photoposition));

        Glide.with(cont)
                .load(photolist.get(photoposition))
                .asBitmap()
                .encoder(new BitmapEncoder(Bitmap.CompressFormat.PNG, 100))
                .diskCacheStrategy(DiskCacheStrategy.SOURCE)
                .dontAnimate()
                .animate(R.anim.alpha)
                .into(iv);

        viewmap.put(position, v);
        container.addView(v);
        return v;
    }

    @Override
    public void destroyItem(ViewGroup container, int position, Object object) {
        if (viewmap.get(position) != null) {
            container.removeView(viewmap.get(position));
        }
    }

    /**
     * 为ViewPager添加item点击监听事件
     */
    private class Itemclicklistener implements View.OnClickListener {

        private int position;

        private Itemclicklistener(int position) {
            this.position = position;
        }

        @Override
        public void onClick(View v) {
            Snackbar.make(bannervp, "第" + position + "张图", Snackbar.LENGTH_SHORT).show();
        }
    }

    /**
     * 我们的图片滑动顺序应该永远是：012012012012012012012   倒序时是021021021021021021021
     * 我们不要21021这样的顺序
     *
     * @return 经过计算，返回轮播图片集合的相对位置
     */
    public static int getPhotoPosition(int viewPagerCurrentItemPosition, int itemSize) {

        if (viewPagerCurrentItemPosition - AdapterForBanner.MAX_BANNER_SIZE / 2 >= 0) {//前进
            return (viewPagerCurrentItemPosition - MAX_BANNER_SIZE / 2) % itemSize;
        } else {//后退
            return (itemSize - ((MAX_BANNER_SIZE / 2 - viewPagerCurrentItemPosition) % itemSize)) % itemSize;
        }
    }
}